<?php

/**
 * weaveQcFormItem actions.
 *
 * @package    WacStorehouse
 * @subpackage weaveQcFormItem
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 *
 * notes:
 * if clone as another one, below tags need to be replace to ur target module tag
 * "weaveQcForm" , "WeaveQcForm", "weave_qc_form"
 */
class weaveQcFormItemActions extends WacCommonActions
{
    /**
     * get searching list
     * @package
     * @author     ben
     */
    public function executeGetList(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $jqGridDataHelper = JqGridDataHelper::getInstance();

        if(($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) && ($request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD)!="")) || $request->hasParameter(JqGridDataHelper::$KEY_SIDX)) {
            $arrParam = array();

            if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) && $request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER)!="") {
                $arrParam['andWhere'] = array(
                        $jqGridDataHelper->getCondMapStr($request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER), "t1.".$request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD), $request->getParameter(JqGridDataHelper::$KEY_SEARCH_STRING))
                );
            }

            if($request->hasParameter(JqGridDataHelper::$KEY_SIDX)) {
                switch($request->getParameter(JqGridDataHelper::$KEY_SIDX)) {
                    case "weave_qc_form_code":
                        $arrParam['orderBy'] = "wwqf.code ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                    case "spinner_name":
                        $arrParam['orderBy'] = "ws.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                    default:
                        $arrParam['orderBy'] = "t1.".$request->getParameter(JqGridDataHelper::$KEY_SIDX)." ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                }
            }

            $resultSet = $this->mainModuleTable->getCustomList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false);
        }
        else {
            $resultSet = $this->mainModuleTable->getCommonList($request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false);
        }

        $filterResultSet = $this->filterList($resultSet);

        $pager = $this->mainModuleTable->getPager();
        $filterResultSet = $jqGridDataHelper->convert($filterResultSet, $pager, JsCommonData::getSuccessDatum());

        return OutputHelper::getInstance()->outputJsonOrTextFormat($filterResultSet, $this);
    }

   /*
    * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['production_order_code']        = $listObj->getWacWeaveQcForm()->getWacProductionOrder()->getCode();
                $tmpArr['weave_qc_form_code']           = $listObj->getWacWeaveQcForm()->getCode();
                $tmpArr['spinner_name']                 = $listObj->getWacSpinner()->getName();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }

    /*
   * @return inspect result
    */
    public function inspectDataValidation(sfWebRequest $request, $params=array()) {
        $result    = JsCommonData::getSuccessDatum();
        $reqParams = $request->getParameterHolder()->getAll();

        if(empty($reqParams['weave_qc_form_id']) || !Doctrine::getTable(WacTable::$weaveQcForm)->isExistedId($reqParams['weave_qc_form_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_weave_qc_form"),  WacErrorCode::$logicError
            );
            return $result;
        }

        if(empty($reqParams['spinner_id']) || !Doctrine::getTable(WacTable::$spinner)->isExistedId($reqParams['spinner_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_spinner"),  WacErrorCode::$logicError
            );
            return $result;
        }

        return $result;
    }

    /*
   * @return list data array
    */
    public function executeAdd(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $objWeaveQcForm = Doctrine::getTable(WacTable::$weaveQcForm)->findOneBy('id', $reqParams['weave_qc_form_id']);
            $targetItem      = $this->mainModuleTable->create();

            if(count($reqParams)>0) {
//              $targetItem->set($reqParams['']);
                $targetItem->setWeaveQcFormId($objWeaveQcForm->getId());

                if(isset($reqParams['weave_qc_form_id']))          {$targetItem->setWeaveQcFormId($reqParams['weave_qc_form_id']);}
                if(isset($reqParams['spinner_id']))                {$targetItem->setSpinnerId($reqParams['spinner_id']);}
                if(isset($reqParams['weave_space']))               {$targetItem->setWeaveSpace($reqParams['weave_space']);}
                if(isset($reqParams['cloth_space']))               {$targetItem->setClothSpace($reqParams['cloth_space']);}
                if(isset($reqParams['quota']))                     {$targetItem->setQuota($reqParams['quota']);}
                if(isset($reqParams['warp_density']))              {$targetItem->setWarpDensity($reqParams['warp_density']);}
                if(isset($reqParams['warp_density_unit_code']))    {$targetItem->setWarpDensityUnitCode($reqParams['warp_density_unit_code']);}
                if(isset($reqParams['filling_density']))           {$targetItem->setFillingDensity($reqParams['filling_density']);}
                if(isset($reqParams['filling_density_unit_code'])) {$targetItem->setFillingDensityUnitCode($reqParams['filling_density_unit_code']);}
                if(isset($reqParams['goods_quantity']))            {$targetItem->setGoodsQuantity($reqParams['goods_quantity']);}
                if(isset($reqParams['texture']))                   {$targetItem->setTexture($reqParams['texture']);}
                if(isset($reqParams['texture_unit_code']))         {$targetItem->setTextureUnitCode($reqParams['texture_unit_code']);}
                if(isset($reqParams['filling_yarn_arrangement']))  {$targetItem->setFillingYarnArrangement($reqParams['filling_yarn_arrangement']);}
                if(isset($reqParams['warp_yarn_arrangement']))     {$targetItem->setWarpYarnArrangement($reqParams['warp_yarn_arrangement']);}
                if(isset($reqParams['beveled']))                   {$targetItem->setBeveled($reqParams['beveled']);}
                if(isset($reqParams['weight']))                    {$targetItem->setWeight($reqParams['weight']);}

                $targetItem->save();

                $this->afterAdd($request, $targetItem);

            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_add_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
   * @return list data array
    */
    public function executeEdit(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $targetItem = $this->mainModuleTable->findOneById($request->getParameter('id'));

            if(count($reqParams)>0) {
                if(isset($reqParams['weave_qc_form_id']))          {$targetItem->setWeaveQcFormId($reqParams['weave_qc_form_id']);}
                if(isset($reqParams['spinner_id']))                {$targetItem->setSpinnerId($reqParams['spinner_id']);}
                if(isset($reqParams['weave_space']))               {$targetItem->setWeaveSpace($reqParams['weave_space']);}
                if(isset($reqParams['cloth_space']))               {$targetItem->setClothSpace($reqParams['cloth_space']);}
                if(isset($reqParams['quota']))                     {$targetItem->setQuota($reqParams['quota']);}
                if(isset($reqParams['warp_density']))              {$targetItem->setWarpDensity($reqParams['warp_density']);}
                if(isset($reqParams['warp_density_unit_code']))    {$targetItem->setWarpDensityUnitCode($reqParams['warp_density_unit_code']);}
                if(isset($reqParams['filling_density']))           {$targetItem->setFillingDensity($reqParams['filling_density']);}
                if(isset($reqParams['filling_density_unit_code'])) {$targetItem->setFillingDensityUnitCode($reqParams['filling_density_unit_code']);}
                if(isset($reqParams['goods_quantity']))            {$targetItem->setGoodsQuantity($reqParams['goods_quantity']);}
                if(isset($reqParams['texture']))                   {$targetItem->setTexture($reqParams['texture']);}
                if(isset($reqParams['texture_unit_code']))         {$targetItem->setTextureUnitCode($reqParams['texture_unit_code']);}
                if(isset($reqParams['filling_yarn_arrangement']))  {$targetItem->setFillingYarnArrangement($reqParams['filling_yarn_arrangement']);}
                if(isset($reqParams['warp_yarn_arrangement']))     {$targetItem->setWarpYarnArrangement($reqParams['warp_yarn_arrangement']);}
                if(isset($reqParams['beveled']))                   {$targetItem->setBeveled($reqParams['beveled']);}
                if(isset($reqParams['weight']))                    {$targetItem->setWeight($reqParams['weight']);}

                $targetItem->save();

                $this->afterEdit($request, $targetItem);

            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_edit_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
     * @return list data array
     */
    public function executeGetFormData(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('weave_qc_form_id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = JsCommonData::getCommonDatum();
        $resultSet['info'] = JsCommonData::getSuccessDatum();

        $objWeaveQcForm = Doctrine::getTable(WacTable::$weaveQcForm)->findOneBy('id', $request->getParameter('weave_qc_form_id'));

        $resultSet['items']['unit'] = Doctrine::getTable(WacTable::$unit)->getCodeNameHash();
        $resultSet['items']['spinner'] = Doctrine::getTable(WacTable::$spinner)->getIdNameHash();
        $resultSet['items']['default']['warp_density_unit_code'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$warpDensityUnitCode);
        $resultSet['items']['default']['filling_density_unit_code'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$fillingDensityUnitCode);
        $resultSet['items']['default']['texture_unit_code'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$textureUnitCode);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }
}